package com.caochenlei.graph;

import java.util.Objects;

public class Edge implements Comparable<Edge> {
    private final int v;            //顶点一
    private final int w;            //顶点二
    private final double weight;    //边权重

    public Edge(int v, int w, double weight) {
        this.v = v;                 //初始化顶点一
        this.w = w;                 //初始化顶点二
        this.weight = weight;       //初始化边权重
    }

    //获取边的权重值
    public double weight() {
        return weight;
    }

    //获取边的一个点
    public int either() {
        return v;
    }

    //获取边的另一个点
    public int other(int vertex) {
        if (vertex == v) {
            return w;
        } else {
            return v;
        }
    }

    @Override
    public int compareTo(Edge that) {
        if (this.weight() > that.weight()) {
            //如果当前边的权重值比that边的权重值大，返回+1
            return +1;
        } else if (this.weight() < that.weight()) {
            //如果当前边的权重值比that边的权重值小，返回-1
            return -1;
        } else {
            //如果当前边的权重值和that边的权重值一样，返回0
            return 0;
        }
    }

    @Override
    public String toString() {
        return String.format("%d-%d %.2f", v, w, weight);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Edge edge = (Edge) o;
        return v == edge.v && w == edge.w && Double.compare(edge.weight, weight) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(v, w, weight);
    }
}
